package chat.util;

import chat.demo.model.ChatHis;
import chat.demo.model.Message;
import com.sun.org.apache.xpath.internal.objects.XString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

@Configuration
public class FileOperation {

    @Value("/Users/zuoyangding/work2")
    private String basePath;

    public static String buildFileName(String filename) {
        return "/Users/zuoyangding/work2" + "/" + filename + ".txt";
    }

    public static boolean checkFileExist(String filename){
        File message_file = new File(filename);
        return message_file.exists();
    }

    public static void storeMessage(String fileName, String message) {
        try {
            if (!checkFileExist(fileName)) {
                System.out.println("==============cannot find the file first time to create============");
                System.out.println(fileName);
                File file = new File(fileName);
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(fileName, true);
            fileWriter.write(message+"\n");
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void storeMessage(String fileName, String message, String userId) {
        try {
            if (!checkFileExist(fileName)) {
                System.out.println("==============cannot find the file first time to create============");
                System.out.println(fileName);
                File file = new File(fileName);
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(fileName, true);
            fileWriter.write(message + "->>>>>" + userId + "\n");
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<ChatHis> readHisMessage(String fileName, String userId) {
        try {
            if (checkFileExist(fileName)) {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
                String chart;
                List<ChatHis> chatHisList = new ArrayList<>();
                while ((chart = bufferedReader.readLine())!=null) {
                    String[] chatArray = chart.split("->>>>>");
                    System.out.println("this is chat message ========= "+ chatArray);
                    ChatHis chatHis = new ChatHis();
                    if (chatArray.length <=1) {
                        chatHis.setMsg(chatArray[0]);
                        chatHis.setUserId(userId);
                    } else {
                        chatHis.setMsg(chatArray[0]);
                        chatHis.setUserId(chatArray[1]);
                    }
                    chatHisList.add(chatHis);
                }
                bufferedReader.close();
                return chatHisList;
            } else {
                System.out.println("=============cannot find the file in file read========");
                System.out.println(fileName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String str;
        Scanner sc = new Scanner(System.in);
        Random random = new Random();
        Message message = new Message();
        float number = random.nextFloat();
        String userId = Float.toString(number);
        message.setUserId(userId);
        while (!(str=sc.next()).equals("0000")) {
            String file_name = buildFileName(userId);
            storeMessage(file_name, str);
            message.setChat(readHisMessage(file_name, null));
        }
        System.out.println(message.getUserId());
        System.out.println(message.getChat());

    }
}
